package com.test.daily.leetcode.y2021.m10.day1027;

import java.util.ArrayDeque;
import java.util.Deque;
import java.util.HashMap;
import java.util.Map;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2021/10/27 下午 03:53
 * @version: 1.0
 */
public class Code02_IsValid {

    public boolean isValid(String s) {
        if(s.length() % 2 != 0){
            return false;
        }
        char[] str = s.toCharArray();
        Deque stack = new ArrayDeque();
        Map<Character,Character> map = new HashMap<Character,Character>(){
            {
                put(']','[');
                put(')','(');
                put('}','{');
            }
        };

        for(int i = 0; i < str.length; i++){
            char ch = str[i];
            if(map.containsKey(ch)){
                if(stack.isEmpty() || stack.peek()!=map.get(ch)){
                    return false;
                }
                stack.pop();
            } else {
                stack.push(ch);
            }
        }
        return stack.isEmpty();
    }
}
